﻿using de.wolfmarco.pas.Shared.Properties;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace de.wolfmarco.pas.Shared.Database
{
    [DbConfigurationType(typeof(DynamicDbConfiguration))]
    public abstract class DynamicDbContext<TContext> : DbContext
        where TContext : DbContext
    {
        public DynamicDbContext(InitializerModeEnum initializerMode)
            : base(Settings.Default.DbConnection, true)
        {
            SetInitializer(initializerMode);
        }

        protected virtual void SetInitializer(InitializerModeEnum initializerMode)
        {
            switch (initializerMode)
            {
                case InitializerModeEnum.None:
                    System.Data.Entity.Database.SetInitializer(new NullDatabaseInitializer<TContext>());
                    break;
                case InitializerModeEnum.DropCreateDatabaseIfModelChanges:
                    System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TContext>());
                    break;
                case InitializerModeEnum.CreateDatabaseIfNotExists:
                    System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<TContext>());
                    break;
                case InitializerModeEnum.DropCreateDatabaseAlways:
                    System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<TContext>());
                    break;
                case InitializerModeEnum.MigrateDatabaseToLatestVersion:
                    throw new InvalidOperationException("TMigrationsConfiguration is missing for " + initializerMode);
                default:
                    throw new NotSupportedException(initializerMode + " is not supported");
            }
        }
    }

    [DbConfigurationType(typeof(DynamicDbConfiguration))]
    public abstract class DynamicDbContext<TContext, TMigrationsConfiguration> : DynamicDbContext<TContext>
        where TContext : DbContext
        where TMigrationsConfiguration : DbMigrationsConfiguration<TContext>, new()
    {
        public DynamicDbContext(InitializerModeEnum initializerMode)
            : base(initializerMode)
        {
            SetInitializer(initializerMode);
        }

        protected override void SetInitializer(InitializerModeEnum initializerMode)
        {
            switch (initializerMode)
            {
                case InitializerModeEnum.MigrateDatabaseToLatestVersion:
                    System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration>());
                    break;
                default:
                    base.SetInitializer(initializerMode);
                    break;
            }
        }
    }
}
